package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.financing.wap.common.CommonConstantUtil;
import com.financing.wap.service.weixin.service.WeixinService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.PrUserAuthDTO;
import com.financing.wap.service.IPrUserAuthService;

@Controller("prUserAuthController")
@RequestMapping("/api/prUserAuth")
public class PrUserAuthController extends BaseController {

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IPrUserAuthService prUserAuthService;

	@Autowired
	private WeixinService weixinJincsServiceImpl;
	
	@RequestMapping("/listPrUserAuth")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, PrUserAuthDTO prUserAuth,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query prUserAuth list begin .......");
			
			prUserAuth.setBegin(pager.getStartIndex());
			prUserAuth.setPageSize(pager.getPageSize());
			
			int count = prUserAuthService.queryListCount(prUserAuth);
			
			pager.setTotalCount(count);
			
			List<PrUserAuthDTO> prUserAuthList = null;
			
			if(count > 0){
				prUserAuthList = prUserAuthService.queryList(prUserAuth);
			}else{
				prUserAuthList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", prUserAuthList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query prUserAuth list end .......");
		}catch(Exception e){
			log.error("query prUserAuth error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addPrUserAuth")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, PrUserAuthDTO prUserAuth){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add prUserAuth  begin .......");
			String openid=request.getParameter("openid");
			//姓名和身份证号码必填
			if(StringUtil.isNullOrEmptyStr(prUserAuth.getUserName(),prUserAuth.getUserIdCard(),openid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			prUserAuth.setAuthStatus("0");
			prUserAuth.setOpenId(openid);
			//身份证正面 反面  手持身份证
			String zmCardPicMediaId=request.getParameter("zmCardPicMediaId");
			String fmCardPicMediaId=request.getParameter("fmCardPicMediaId");
			String scCardPicMediaId=request.getParameter("scCardPicMediaId");

			//微信图片上传
			if(StringUtils.isNotBlank(zmCardPicMediaId)){
				String picAddr = weixinJincsServiceImpl.getFileByMediaId("jpg", zmCardPicMediaId);
				prUserAuth.setFrontIdCard(CommonConstantUtil.OSS_PATH+picAddr);
			}
			if(StringUtils.isNotBlank(fmCardPicMediaId)){
				String picAddr = weixinJincsServiceImpl.getFileByMediaId("jpg", fmCardPicMediaId);
				prUserAuth.setReverseIdCard(CommonConstantUtil.OSS_PATH+picAddr);
			}
			if(StringUtils.isNotBlank(scCardPicMediaId)){
				String picAddr = weixinJincsServiceImpl.getFileByMediaId("jpg", scCardPicMediaId);
				prUserAuth.setAllIdCard(CommonConstantUtil.OSS_PATH+picAddr);
			}

			DTOdateSetUtil.setCreateInfo(prUserAuth);
			DTOdateSetUtil.setUpdateInfo(prUserAuth);
			
			int id = prUserAuthService.insert(prUserAuth);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add prUserAuth list end .......");
		}catch(Exception e){
			log.error("add prUserAuth error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}


	@RequestMapping("/detailPrUserAuth")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, PrUserAuthDTO prUserAuth){
		ResultMap result = new ResultMap();

		try{
			logger.info("query detail prUserAuth  begin .......");

			Integer id = prUserAuth.getId();

			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			PrUserAuthDTO dto = prUserAuthService.loadById(id);

			result.setAttribute("dto",dto);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query detail prUserAuth  end .......");
		}catch(Exception e){
			log.error("query detail prUserAuth error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/updatePrUserAuth")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, PrUserAuthDTO prUserAuth){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update prUserAuth  begin .......");
			
			Integer id = prUserAuth.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			String openid=request.getParameter("openid");
			//姓名和身份证号码必填
			if(StringUtil.isNullOrEmptyStr(prUserAuth.getUserName(),prUserAuth.getUserIdCard(),openid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			//查询
			PrUserAuthDTO dto = prUserAuthService.loadById(id);
			if(!dto.getOpenId().equals(openid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			//待审核和失败
			if("0".equals(dto.getAuthStatus())||"1".equals(dto.getAuthStatus())){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			prUserAuth.setOpenId(openid);
			//身份证正面 反面  手持身份证
			String zmCardPicMediaId=request.getParameter("zmCardPicMediaId");
			String fmCardPicMediaId=request.getParameter("fmCardPicMediaId");
			String scCardPicMediaId=request.getParameter("scCardPicMediaId");

			//微信图片上传
			if(StringUtils.isNotBlank(zmCardPicMediaId)){
				String picAddr = weixinJincsServiceImpl.getFileByMediaId("jpg", zmCardPicMediaId);
				prUserAuth.setFrontIdCard(CommonConstantUtil.OSS_PATH+picAddr);
			}
			if(StringUtils.isNotBlank(fmCardPicMediaId)){
				String picAddr = weixinJincsServiceImpl.getFileByMediaId("jpg", fmCardPicMediaId);
				prUserAuth.setReverseIdCard(CommonConstantUtil.OSS_PATH+picAddr);
			}
			if(StringUtils.isNotBlank(scCardPicMediaId)){
				String picAddr = weixinJincsServiceImpl.getFileByMediaId("jpg", scCardPicMediaId);
				prUserAuth.setAllIdCard(CommonConstantUtil.OSS_PATH+picAddr);
			}

			DTOdateSetUtil.setUpdateInfo(prUserAuth);
			
			prUserAuthService.update(prUserAuth);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update prUserAuth list end .......");
		}catch(Exception e){
			log.error("update prUserAuth error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}

}
